using UnityEngine;
using UnityEditor;
using UnityEditorInternal;

namespace Games.Frameworks.Editor
{
    /// <summary>
    /// 添加设置Tag或Layer
    /// </summary>
    public class EditorLayerAndTag
    {
        /// <summary>
        /// 添加或者创建新的标签
        /// </summary>
        /// <param name="tag">标签名</param>
        /// <param name="obj">需要添加的对象</param>
        public static void AddOrCreateMyNewTag(string tag, GameObject obj)
        {
            if (isHasTag(tag))
            {
                obj.tag = tag;
            }
            else
            {
                SerializedObject tagManager = new SerializedObject(obj); //序列化物体
                SerializedProperty it = tagManager.GetIterator(); //序列化属性
                while (it.NextVisible(true)) //下一属性的可见性
                {
                    if (it.name == "m_TagString")
                    {
                        //Debug.Log(it.stringValue);
                        it.stringValue = tag;
                        tagManager.ApplyModifiedProperties();
                    }
                }
            }
        }

        /// <summary>
        /// 添加或者创建新的标签
        /// </summary>
        /// <param name="tag">标签名</param>
        /// <param name="obj">需要添加的对象</param>
        public static void AddOrCreateNewTag(string tag, GameObject obj)
        {
            if (!isHasTag(tag))
            {
                InternalEditorUtility.AddTag(tag);
            }

            obj.tag = tag;
        }

        /// <summary>
        /// 是否有标签
        /// </summary>
        /// <param name="tag">标签名</param>
        /// <returns>判断结果</returns>
        public static bool isHasTag(string tag)
        {
            string[] tags = InternalEditorUtility.tags;
            for (int i = 0; i < tags.Length; i++)
            {
                if (tags[i].Equals(tag))
                    return true;
            }

            return false;
        }

        /// <summary>
        /// 添加或者创建新的层标记
        /// </summary>
        /// <param name="layer">标记层名</param>
        /// <param name="obj">需要添加的对象</param>
        public static void AddOrCreateMyNewLayer(string layer, GameObject obj)
        {
            if (!isHasLayer(layer))
            {
                SerializedObject tagManager =
                    new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/Tagmanager.asset"));
                SerializedProperty it = tagManager.GetIterator();
                while (it.NextVisible(true))
                {
                    if (it.name == "layers")
                    {
                        for (int i = 8; i < it.arraySize; i++)
                        {
                            SerializedProperty sp = it.GetArrayElementAtIndex(i);
                            if (string.IsNullOrEmpty(sp.stringValue))
                            {
                                sp.stringValue = layer;
                                tagManager.ApplyModifiedProperties();
                                //Debug.Log("success");
                                break;
                            }
                        }

                        break;
                    }
                }
            }

            obj.layer = LayerMask.NameToLayer(layer);
        }

        /// <summary>
        /// 是否有层标记
        /// </summary>
        /// <param name="layer">标记层名</param>
        /// <returns>判断结果</returns>
        public static bool isHasLayer(string layer)
        {
            string[] layers = InternalEditorUtility.layers;
            for (int i = 0; i < layers.Length; i++)
            {
                if (layers[i].Contains(layer))
                    return true;
            }

            return false;
        }
        
    }
}
